package panel

import (
	"image"
	"image/color"
)
import "freetype-go.googlecode.com/hg/freetype"
import "freetype-go.googlecode.com/hg/freetype/raster"
import "freetype-go.googlecode.com/hg/freetype/truetype"

type Pen interface {
	Color() *image.Uniform
	Font() *truetype.Font
}

type Paper interface {
	PaperColor() *image.Uniform
}

type PlainPaper struct {
	Color *image.Uniform
}

func (paper PlainPaper) PaperColor() *image.Uniform {
	return paper.Color
}

type PlainPen struct {
	colourImage *image.Uniform
	font        *truetype.Font
}

func (pp *PlainPen) Color() *image.Uniform {
	return pp.colourImage
}

func (pp *PlainPen) Font() *truetype.Font {
	return pp.font
}

func NewPlainPen(font *truetype.Font, r, g, b uint8) *PlainPen {
	return &PlainPen{image.NewUniform(color.RGBA{r, g, b, 255}), font}
}

type DrawInfo struct {
	context       *freetype.Context
	tfont         *truetype.Font
	pixelsPerLine int
	pixelsPerCol  int
	char_height   int
	pixelTweak    int
	Pens          map[string]Pen
	Thingy        Pen
	Back          Paper
	Cream         Paper
}

func (di *DrawInfo) CellsToPixels(lines, cols int) (width, height int) {
	return cols * di.pixelsPerCol, lines * di.pixelsPerLine
}

func (di *DrawInfo) CellsToFix32(lines, cols int) Point {
	return Point{raster.Fix32(cols*di.pixelsPerCol) << 8, raster.Fix32(lines*di.pixelsPerLine) << 8}
}

func (di *DrawInfo) Fix32ToCells(xy Point) (lines, cols int) {
	return int(xy.Y>>8) / di.pixelsPerLine, int(xy.X>>8) / di.pixelsPerCol
}

func (di *DrawInfo) Fix32ToPixels(xy Point) (x, y int) {
	return int(xy.X >> 8), int(xy.Y >> 8)
}

func (di *DrawInfo) PixelsToFix32(x, y int) (xy Point) {
	return Point{raster.Fix32(x << 8), raster.Fix32(y << 8)}
}

func (di *DrawInfo) PixelsToCells(width, height int) (rows, cols int) {
	// Log("DrawInfo.PixelsToCells: ", di)
	return height / di.pixelsPerLine, width / di.pixelsPerCol
}
